package com.eco.schedule;

import com.alibaba.fastjson.JSONArray;
import com.eco.entity.Sensor;
import com.eco.service.SensorService;
import com.eco.utils.DateAndHexUtil;
import com.eco.utils.NettyCtxMap;
import com.eco.websocket.WebSocketServer;
import com.github.benmanes.caffeine.cache.Cache;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
/* loaded from: newfishway-1.0.0-SNAPSHOT.jar:BOOT-INF/classes/com/eco/schedule/ScheduleTask.class */
public class ScheduleTask {
    private static final Logger log = LoggerFactory.getLogger((Class<?>) ScheduleTask.class);
    private static final String hdswCommand = "01 03 02 58 00 04 C4 62";
    private static final String lsCommand = "01 03 00 12 00 0A 65 C8";
    private Long startTime = null;

    @Value("${sensor.elevation-jk}")
    private BigDecimal elevationJK;

    @Value("${sensor.elevation-ck}")
    private BigDecimal elevationCK;

    @Value("${sensor.jklsz}")
    private BigDecimal jklsz;

    @Value("${sensor.cklsz}")
    private BigDecimal cklsz;

    @Autowired
    private SensorService sensorService;

    @Resource
    private Cache<String, Object> caffeineCache;

    public void sendSerialPortCommand2() {
        try {
            ChannelHandlerContext ctx6 = NettyCtxMap.get("sensor6");
            if (ctx6 != null) {
                String msg = new String(hdswCommand.getBytes(), "utf-8");
                ctx6.writeAndFlush(msg);
            }
        } catch (UnsupportedEncodingException e) {
            log.error(e.toString());
        }
    }

    @Scheduled(initialDelay = 2000, fixedRate = 5000)
    public void sendSerialPortCommand() {
        if (this.startTime == null) {
            this.startTime = Long.valueOf(System.currentTimeMillis());
        }
        try {
            try {
                JSONArray result = new JSONArray(9);
                JSONArray jkls = (JSONArray) this.caffeineCache.getIfPresent("jkls");
                JSONArray ckls = (JSONArray) this.caffeineCache.getIfPresent("ckls");
                if (jkls == null) {
                    result.add("0.00");
                    result.add("0.00");
                } else {
                    result.add(new BigDecimal(jkls.getString(0)).add(this.elevationJK).setScale(2, 1).toString());
                    result.add(new BigDecimal(jkls.getString(1)).multiply(this.jklsz).setScale(2, 1).toString());
                }
                if (ckls == null) {
                    result.add("0.00");
                    result.add("0.00");
                } else {
                    result.add(new BigDecimal(ckls.getString(0)).add(this.elevationCK).setScale(2, 1).toString());
                    result.add(new BigDecimal(ckls.getString(1)).multiply(this.cklsz).setScale(2, 1).toString());
                }
                if (jkls == null && ckls == null) {
                    WebSocketServer.sendInfo("false", "bl");
                } else {
                    WebSocketServer.sendInfo(result.toJSONString(), "bl");
                    if (System.currentTimeMillis() - this.startTime.longValue() >= 600000) {
                        Sensor sensor = new Sensor();
                        sensor.setStime(DateAndHexUtil.dateTimeNow());
                        sensor.setJksw(Double.valueOf(result.getString(0)));
                        sensor.setJkls(Double.valueOf(result.getString(1)));
                        sensor.setCksw(Double.valueOf(result.getString(2)));
                        sensor.setCkls(Double.valueOf(result.getString(3)));
                        this.sensorService.addByScan(sensor);
                        this.startTime = null;
                    }
                }
                try {
                    ChannelHandlerContext ctx2 = NettyCtxMap.get("sensor2");
                    ChannelHandlerContext ctx3 = NettyCtxMap.get("sensor3");
                    if (ctx2 != null) {
                        String msg = new String(lsCommand.getBytes(), "utf-8");
                        ctx2.writeAndFlush(msg);
                    }
                    if (ctx3 != null) {
                        String msg2 = new String(lsCommand.getBytes(), "utf-8");
                        ctx3.writeAndFlush(msg2);
                    }
                } catch (UnsupportedEncodingException e) {
                    log.error(e.toString());
                }
            } catch (Throwable th) {
                try {
                    ChannelHandlerContext ctx22 = NettyCtxMap.get("sensor2");
                    ChannelHandlerContext ctx32 = NettyCtxMap.get("sensor3");
                    if (ctx22 != null) {
                        String msg3 = new String(lsCommand.getBytes(), "utf-8");
                        ctx22.writeAndFlush(msg3);
                    }
                    if (ctx32 != null) {
                        String msg4 = new String(lsCommand.getBytes(), "utf-8");
                        ctx32.writeAndFlush(msg4);
                    }
                } catch (UnsupportedEncodingException e2) {
                    log.error(e2.toString());
                }
                throw th;
            }
        } catch (Exception e3) {
            log.error(e3.toString());
            try {
                ChannelHandlerContext ctx23 = NettyCtxMap.get("sensor2");
                ChannelHandlerContext ctx33 = NettyCtxMap.get("sensor3");
                if (ctx23 != null) {
                    String msg5 = new String(lsCommand.getBytes(), "utf-8");
                    ctx23.writeAndFlush(msg5);
                }
                if (ctx33 != null) {
                    String msg6 = new String(lsCommand.getBytes(), "utf-8");
                    ctx33.writeAndFlush(msg6);
                }
            } catch (UnsupportedEncodingException e4) {
                log.error(e4.toString());
            }
        }
    }

    private ByteBuf getByteBuf(ChannelHandlerContext ctx, String s) {
        ByteBuf buffer = ctx.alloc().buffer(s.getBytes().length * 2);
        buffer.writeBytes(s.getBytes(Charset.forName("utf-8")));
        return buffer;
    }
}